#include "writer.h"
#include <iostream>
#include <algorithm>
#include <functional>
#ifdef WIN32
#include <windows.h>
#include <tchar.h>
#elif APPLE
#define _tcslen wcslen
#endif
#include <assert.h>

namespace AllTing
{

namespace Test
{

void IWritable::AddWriter(IWritable* pWritable)
{
}

void IWritable::RemoveWriter(IWritable* pWritable)
{
}

void CompositeWritable::AddWriter(IWritable* pWritable)
{
	assert(pWritable);
	if(!pWritable)
		return;

	WritableIterator it = std::find(vWritables_.begin(), vWritables_.end(), pWritable);

	if(it == vWritables_.end())
		vWritables_.push_back(pWritable);
}

void CompositeWritable::RemoveWriter(IWritable* pWritable)
{
	assert(pWritable);
	if(!pWritable)
		return;

	std::remove(vWritables_.begin(), vWritables_.end(), pWritable);
}

bool CompositeWritable::empty() const
{
    return vWritables_.empty();
}

UInt32 CompositeWritable::Write(const Char* pszLog) const
{
	std::for_each(vWritables_.begin(), vWritables_.end(), std::bind2nd(std::mem_fun(&IWritable::Write), pszLog));

	return (UInt32)_tcslen(pszLog);
}

UInt32 StdOutWriter::Write(const Char* pszLog) const
{
#ifdef UNICODE
	std::wcout << pszLog;
#else
	std::cout << pszLog;
#endif
	return (UInt32)_tcslen(pszLog);
}


UInt32 OutputDebugStringWriter::Write(const Char* pszLog) const
{
#ifdef WIN32
	::OutputDebugString(pszLog);
#endif
	
	return (UInt32)_tcslen(pszLog);
}

	
}	// namespace Test

}	// namespace AllTing